This guide walks you through configuring Nginx on an Ubuntu server to host a simple website. Assuming Nginx is already installed, the tutorial covers creating a website directory, setting up a server block, and ensuring your site is accessible via domain or IP address. Perfect for beginners in server management.
25 min
Edited:16-09-2024
Nginx is a popular and efficient web server that can serve static and dynamic content. In this tutorial, we’ll focus on configuring Nginx to host a simple website. Let’s get started with the assumption that Nginx is already installed on your Ubuntu server.
If you did not install nginx already, you can follow this guide to install it:
First, you need a directory to store your website’s files. This is where Nginx will look for the files to serve.
1. sudo mkdir -p /var/www/yourdomain.com/html
Now, give the ownership of this directory to the current user:
1. sudo chown -R $USER:$USER /var/www/yourdomain.com/html
Set appropriate permissions:
1. sudo chmod -R 755 /var/www/yourdomain.com
To verify that your server is serving content correctly, create a simple HTML file.
1. nano /var/www/yourdomain.com/html/index.html
Paste the following content into the file:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Welcome to Your Website!
5. </title>
6. </head>
7. <body>
8. <h1>Success! Your website is up and running!</h1>
9. </body>
10. </html>
Save and exit the file.
Nginx uses server blocks (similar to Apache’s virtual hosts) to route traffic to the correct directory. You can either modify the default server block or create a new one. Let’s create a new one for your domain.
1. sudo nano /etc/nginx/sites-available/yourdomain.com
Add the following configuration:
1. server {
2. listen 80;
3. server_name yourdomain.com www.yourdomain.com;
4.
5. root /var/www/yourdomain.com/html;
6. index index.html;
7.
8. location / {
9. try_files $uri $uri/ =404;
10. }
11. }
12.
This configuration specifies that Nginx will listen on port 80 for traffic directed to yourdomain.com or www.yourdomain.com, and serve files from the /var/www/yourdomain.com/html directory.
To enable the new configuration, you need to create a symbolic link from the configuration file in sites-available to the sites-enabled directory.
1. sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Before restarting Nginx, test the configuration to ensure there are no syntax errors:
1. sudo nginx -t
If the test is successful, you should see a message saying the configuration is OK.
To apply the changes, restart Nginx:
1. sudo systemctl restart nginx
Make sure that your domain is pointing to the server’s IP address by updating the DNS records with your domain registrar.
Once everything is set up, you can open a browser and navigate to your domain (or the server’s public IP address) to see your website.
1. http://yourdomain.com
You should see the message "Success! Your website is up and running!" from your HTML file.
This guide walks you through configuring Nginx on an Ubuntu server to host a simple website. Assuming Nginx is already installed, the tutorial covers creating a website directory, setting up a server block, and ensuring your site is accessible via domain or IP address. Perfect for beginners in server management.
What's next
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min